﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace dll_Csharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int result = 0;
        int netprotocol = 1;
        int updatestyle = 1;
        bool save = false;
        bool flash = false;
        int comport = 1;
        int ipport = 1;
        int baud = 1;
        int ledcolour = 1;
        int width = 1;
        int higth = 1;
        string text = "";
        string voicetext = "";
        int UID = 1;
        int colour = 1;
        int font = 1;
        int size = 1;
        string ip = "";
        int pagenumber = 1;
        int style = 1;
        int speed = 1;
        int stoptime = 1;
        string imgpath = "";
        int brightness = 1;
        int timeformat = 0;
        int showformat = 0;
        //485地址
        string rsadr = "1";
        //1=串口，2=485
        int rstype = 1;

        //串口通讯回调函数
        private void comCallBackFun(int comMsg)
        {
            if (comMsg == 0)
            {
                MessageBox.Show("下发成功！", "发送内容");
            }
            else
            {
                MessageBox.Show("下发失败！", "发送内容");
            }
        }
        //选中udp通讯时动作
        private void radioButtonuUDP_CheckedChanged(object sender, EventArgs e)
        {
            textBoxIPPort.Enabled = false;
            textBoxIP.Enabled = true;
            buttonOpenServer.Enabled = false;
            buttonCloseServer.Enabled = false;
            comboBoxCOMPort.Enabled = false;
            comboBoxBaudRate.Enabled = false;
            textBoxRSAdr.Enabled = false;

            radioButtonZFCOM1.Enabled = true;
            radioButtonZFCOM2.Enabled = true;
            radioButtonZF485.Enabled = true;
            radioButtonBZF.Enabled = true;
            
        }
        //选中tcp通讯时动作
        private void radioButtonTCP_CheckedChanged(object sender, EventArgs e)
        {
            textBoxIPPort.Enabled = true;
            textBoxIP.Enabled = true;
            buttonOpenServer.Enabled = true;
            buttonCloseServer.Enabled = true;
            comboBoxCOMPort.Enabled = false;
            comboBoxBaudRate.Enabled = false;
            textBoxRSAdr.Enabled = false;

            radioButtonZFCOM1.Enabled = true;
            radioButtonZFCOM2.Enabled = true;
            radioButtonZF485.Enabled = true;
            radioButtonBZF.Enabled = true;
        }
        //选中com通讯时动作
        private void radioButtonCOM_CheckedChanged(object sender, EventArgs e)
        {
            textBoxIPPort.Enabled = false;
            textBoxIP.Enabled = false;
            buttonOpenServer.Enabled = false;
            buttonCloseServer.Enabled = false;
            comboBoxCOMPort.Enabled = true;
            comboBoxBaudRate.Enabled = true;
            textBoxRSAdr.Enabled = true;

            radioButtonZFCOM1.Enabled = false;
            radioButtonZFCOM2.Enabled = false;
            radioButtonZF485.Enabled = false;
            radioButtonBZF.Enabled = false;
        }
        //选中485通讯时动作
        private void radioButton485_CheckedChanged(object sender, EventArgs e)
        {
            textBoxIPPort.Enabled = false;
            textBoxIP.Enabled = false;
            buttonOpenServer.Enabled = false;
            buttonCloseServer.Enabled = false;
            comboBoxCOMPort.Enabled = true;
            comboBoxBaudRate.Enabled = true;
            textBoxRSAdr.Enabled = true;

            radioButtonZFCOM1.Enabled = false;
            radioButtonZFCOM2.Enabled = false;
            radioButtonZF485.Enabled = false;
            radioButtonBZF.Enabled = false;
        }

        //选中内码文字时动作
        private void radioButtonNB_CheckedChanged(object sender, EventArgs e)
        {
            comboBoxSave.Enabled = true;
            comboBoxUpdateStyle.Enabled = true;
            comboBoxFont.Enabled = true;
            comboBoxSize.Enabled = true;
            comboBoxFlash.Enabled = false;
            comboBoxStyle.Enabled = true;
            comboBoxSpeed.Enabled = true;
            comboBoxStopTime.Enabled = true;
            comboBoxColour.Enabled = true;
            buttonImg.Enabled = false;
            textBoxImgPath.Enabled = false;
            textBoxText.Enabled = true;
            comboBoxTimeFormat.Enabled = false;
            comboBoxShowFormat.Enabled = false;
        }
        //选中实时采集时动作
        private void radioButtonCJ_CheckedChanged(object sender, EventArgs e)
        {
            comboBoxSave.Enabled = false;
            comboBoxUpdateStyle.Enabled = false;
            comboBoxFont.Enabled = true;
            comboBoxSize.Enabled = true;
            comboBoxFlash.Enabled = false;
            comboBoxStyle.Enabled = false;
            comboBoxSpeed.Enabled = false;
            comboBoxStopTime.Enabled = false;
            comboBoxColour.Enabled = true;
            buttonImg.Enabled = false;
            textBoxImgPath.Enabled = false;
            textBoxText.Enabled = true;
            comboBoxTimeFormat.Enabled = false;
            comboBoxShowFormat.Enabled = false;
        }
        //选中排队叫号时动作
        private void radioButtonPD_CheckedChanged(object sender, EventArgs e)
        {
            comboBoxSave.Enabled = false;
            comboBoxUpdateStyle.Enabled = false;
            comboBoxFont.Enabled = false;
            comboBoxSize.Enabled = false;
            comboBoxFlash.Enabled = true;
            comboBoxStyle.Enabled = false;
            comboBoxSpeed.Enabled = false;
            comboBoxStopTime.Enabled = true;
            comboBoxColour.Enabled = true;
            buttonImg.Enabled = false;
            textBoxImgPath.Enabled = false;
            textBoxText.Enabled = true;
            comboBoxTimeFormat.Enabled = false;
            comboBoxShowFormat.Enabled = false;
        }
        //选中日期时间时动作
        private void radioButtonTime_CheckedChanged(object sender, EventArgs e)
        {
            comboBoxSave.Enabled = false;
            comboBoxUpdateStyle.Enabled = false;
            comboBoxFont.Enabled = true;
            comboBoxSize.Enabled = true;
            comboBoxFlash.Enabled = false;
            comboBoxStyle.Enabled = false;
            comboBoxSpeed.Enabled = false;
            comboBoxStopTime.Enabled = false;
            comboBoxColour.Enabled = true;
            buttonImg.Enabled = false;
            textBoxImgPath.Enabled = false;
            textBoxText.Enabled = false;
            comboBoxTimeFormat.Enabled = true;
            comboBoxShowFormat.Enabled = true;
        }
        //选中图片组时动作
        private void radioButtonImg_CheckedChanged(object sender, EventArgs e)
        {
            comboBoxSave.Enabled = true;
            comboBoxUpdateStyle.Enabled = false;
            comboBoxFont.Enabled = false;
            comboBoxSize.Enabled = false;
            comboBoxFlash.Enabled = false;
            comboBoxStyle.Enabled = true;
            comboBoxSpeed.Enabled = true;
            comboBoxStopTime.Enabled = true;
            comboBoxColour.Enabled = false;
            buttonImg.Enabled = true;
            textBoxImgPath.Enabled = true;
            textBoxText.Enabled = false;
            comboBoxTimeFormat.Enabled = false;
            comboBoxShowFormat.Enabled = false;
        }
        //选取图片路径函数
        private void buttonImg_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Img file(*.bmp)|*.bmp";
            openFileDialog1.Multiselect = false;
            openFileDialog1.Title = "Open Img file ";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textBoxImgPath.Text = openFileDialog1.FileName;
            }
            openFileDialog1.Dispose();
        }
        //开启服务
        private void buttonOpenServer_Click(object sender, EventArgs e)
        {
            ipport = Convert.ToInt32(textBoxIPPort.Text);
            result = 1;
            result = QYLED_DLL.OpenServer(ipport);
            if (result == 0)
            {
                MessageBox.Show("开启成功！", "开启服务");
            }
            else
            {
                MessageBox.Show("开启失败！", "开启服务");
            }
        }
        //关闭服务
        private void buttonCloseServer_Click(object sender, EventArgs e)
        {
            result = 1;
            result = QYLED_DLL.CloseServer();
            if (result == 0)
            {
                MessageBox.Show("关闭成功！", "关闭服务");
            }
            else
            {
                MessageBox.Show("关闭失败！", "关闭服务");
            }
        }
        //添加显示页
        private void buttonAddPage_Click(object sender, EventArgs e)
        {
            string starttime = "00:00";  //开始播放时间
            string stoptime = "23:59";    //结束播放时间
            int day = 0;                  //0=天天播放，1=周一播放，2=周二播放，3=…………
            result = 1;
            result = QYLED_DLL.AddShowPage(starttime, stoptime, day);
            if (result == 0)
            {
                MessageBox.Show("添加成功！", "添加显示页");
            }
            else
            {
                MessageBox.Show("添加失败！", "添加显示页");
            }
        }
        //添加区域
        private void buttonAddArea_Click(object sender, EventArgs e)
        {
            int x = 0;        //x坐标
            int y = 0;        //y坐标
            width = Convert.ToInt32(textBoxWidth.Text);//宽度
            higth = Convert.ToInt32(textBoxHigth.Text);//高度
            result = 1;
            result = QYLED_DLL.AddArea(x, y, width, higth);
            if (result == 0)
            {
                MessageBox.Show("添加成功！", "添加区域");
            }
            else
            {
                MessageBox.Show("添加失败！", "添加区域");
            }
        }
        //添加素材
        private void buttonAddMaterial_Click(object sender, EventArgs e)
        {
            ledcolour = comboBoxLEDcolour.SelectedIndex;    //led颜色
            width = Convert.ToInt32(textBoxWidth.Text);     //区域宽度
            higth = Convert.ToInt32(textBoxHigth.Text);     //区域高度
            colour = comboBoxColour.SelectedIndex + 1;      //显示内容颜色
            font = comboBoxFont.SelectedIndex + 1;          //字体
            size = comboBoxSize.SelectedIndex;              //字号
            style = comboBoxStyle.SelectedIndex;            //显示方式
            speed = comboBoxSpeed.SelectedIndex;            //显示速度
            stoptime = comboBoxStopTime.SelectedIndex;      //停留时间
            text = textBoxText.Text.Trim();                 //显示内容
            UID = Convert.ToInt32(textBoxUID.Text.Trim());  //素材uid
            imgpath = textBoxImgPath.Text;                  //图片路径
            timeformat = comboBoxTimeFormat.SelectedIndex + 1;  //日期时间长度
            showformat = comboBoxShowFormat.SelectedIndex + 1;  //日期时间格式

            int numcolour = colour;   //数字颜色
            int chrcolour = colour;   //字符颜色
            int yearlen = 0;          //=0 年四位; =1年两位
            int timedifset = 1;       //=0 滞后  =1超前       
            int hourspan = 0;         //滞后或超前小时数
            int minspan = 0;          //滞后或超前分钟数

            if (radioButtonNB.Checked)
            {
                //添加内码文字素材
                result = 1;
                result = QYLED_DLL.AddTemplate_InternalText(text, UID, ledcolour, style, speed, stoptime, colour, font, size, save);
                if (result == 0)
                {
                    MessageBox.Show("添加成功！", "添加内码文字素材");
                }
                else
                {
                    MessageBox.Show("添加失败！", "添加内码文字素材");
                }
            }
            else if (radioButtonCJ.Checked)
            {
                //添加实时采集素材
                result = 1;
                result = QYLED_DLL.AddTemplate_CollectData(text, UID, ledcolour, colour, font, size);
                if (result == 0)
                {
                    MessageBox.Show("添加成功！", "添加实时采集素材");
                }
                else
                {
                    MessageBox.Show("添加失败！", "添加实时采集素材");
                }
            }
            else if (radioButtonPD.Checked)
            {
                //添加排队叫号素材
                result = 1;
                result = QYLED_DLL.AddTemplate_LineUp(text, stoptime, colour, font, size, UID, flash);
                if (result == 0)
                {
                    MessageBox.Show("添加成功！", "添加排队叫号素材");
                }
                else
                {
                    MessageBox.Show("添加失败！", "添加排队叫号素材");
                }
            }
            else if (radioButtonTime.Checked)
            {
                //添加日期时间素材
                result = 1;
                result = QYLED_DLL.AddTemplate_DateTime(UID, ledcolour, numcolour, chrcolour, font, size, yearlen, timeformat, showformat, timedifset, hourspan, minspan, stoptime, save);//单基色时下发正常，双基色时报错。
                if (result == 0)
                {
                    MessageBox.Show("添加成功！", "添加日期时间素材");
                }
                else
                {
                    MessageBox.Show("添加失败！", "添加日期时间素材");
                }
            }
            else if (radioButtonImg.Checked)
            {
                //添加图片组素材
                result = 1;
                result = QYLED_DLL.AddTemplate_ImageGroup(imgpath, UID, ledcolour, style, speed, stoptime);
                if (result == 0)
                {
                    MessageBox.Show("添加成功！", "添加素图片组材");
                }
                else
                {
                    MessageBox.Show("添加失败！", "添加图片组素材");
                }
            }
        }
        //发送模板
        private void buttonSendFormwork_Click(object sender, EventArgs e)
        {
            ip = textBoxIP.Text.Trim();                       //ip地址

            if (radioButtonUDP.Checked)
            {
                //udp发送模板
                netprotocol = 1;
                result = 1;
                result = QYLED_DLL.SendTemplateData_Net(ip, netprotocol);

                if (result == 0)
                {
                    MessageBox.Show("发送成功！", "udp发送模板");
                }
                else
                {
                    MessageBox.Show("发送失败！", "udp发送模板");
                }
            }
            if (radioButtonTCP.Checked)
            {
                //tcp发送模板
                netprotocol = 2;
                result = 1;
                result = QYLED_DLL.SendTemplateData_Net(ip, netprotocol);

                if (result == 0)
                {
                    MessageBox.Show("发送成功！", "tcp发送模板");
                }
                else
                {
                    MessageBox.Show("发送失败！", "tcp发送模板");
                }
            }
            if (radioButtonCOM.Checked)
            {
                //无此接口
            }
        }
        //下发显示内容（需建模板）
        private void buttonSendScreen_Click(object sender, EventArgs e)
        {
            ip = textBoxIP.Text.Trim();                         //ip地址
            ipport = Convert.ToInt32(textBoxIPPort.Text);       //tcp服务端口
            comport = comboBoxCOMPort.SelectedIndex + 1;        //com端口
            baud = Convert.ToInt32(comboBoxBaudRate.Text);      //波特率
            ledcolour = comboBoxLEDcolour.SelectedIndex;        //led颜色
            width = Convert.ToInt32(textBoxWidth.Text);         //区域宽度
            higth = Convert.ToInt32(textBoxHigth.Text);         //区域高度
            colour = comboBoxColour.SelectedIndex + 1;          //显示颜色
            font = comboBoxFont.SelectedIndex + 1;              //字体
            size = comboBoxSize.SelectedIndex;                  //字号
            style = comboBoxStyle.SelectedIndex;                //显示方式
            speed = comboBoxSpeed.SelectedIndex;                //移动速度
            stoptime = comboBoxStopTime.SelectedIndex;          //停留时间
            updatestyle = comboBoxUpdateStyle.SelectedIndex + 1;//更新方式
            timeformat = comboBoxTimeFormat.SelectedIndex+1;    //时间格式
            showformat = comboBoxShowFormat.SelectedIndex+1;    //显示格式
            text = textBoxText.Text;                            //显示内容
            UID = Convert.ToInt32(textBoxUID.Text.Trim());      //素材uid
            imgpath = textBoxImgPath.Text.Trim();               //图片路径
            rsadr = textBoxRSAdr.Text;                          //485地址
            if (comboBoxSave.SelectedIndex == 0)                //掉电保存
            {
                save = true;
            }
            else
            {
                save = false;
            }
            if (comboBoxFlash.SelectedIndex == 0)               //排队叫号闪烁
            {
                flash = true;
            }
            else
            {
                flash = false;
            }
            
            //udp发送方式
            if (radioButtonUDP.Checked)
            {
                netprotocol = 1;
                if (radioButtonNB.Checked)
                {
                    //udp下发内码文字内容
                    result = 1;
                    result = QYLED_DLL.SendInternalText_Net(text, ip, netprotocol, width, higth, UID, ledcolour, style, speed, stoptime, colour, font, size, updatestyle, save);
                    if (result == 0)
                    {
                        MessageBox.Show("下发成功！", "udp发送内码文字内容");
                    }
                    else
                    {
                        MessageBox.Show("下发失败！", "udp发送内码文字内容");
                    }
                }
                else if (radioButtonCJ.Checked)
                {
                    //udp下发实时采集内容
                    result = 1;
                    result = QYLED_DLL.SendCollectionData_Net(text, ip, netprotocol, UID, colour, font, size);
                    if (result == 0)
                    {
                        MessageBox.Show("下发成功！", "udp发送实时采集内容");
                    }
                    else
                    {
                        MessageBox.Show("下发失败！", "udp发送实时采集内容");
                    }
                }
                else if (radioButtonPD.Checked)
                {
                    //udp下发排队叫号内容
                    result = 1;
                    result = QYLED_DLL.SendLineUp_Net(text, ip, netprotocol, stoptime, colour, UID, flash);
                    if (result == 0)
                    {
                        MessageBox.Show("下发成功！", "udp发送排队叫号内容");
                    }
                    else
                    {
                        MessageBox.Show("下发失败！", "udp发送排队叫号内容");
                    }
                }
                else if (radioButtonTime.Checked)
                {
                    //udp下发日期时间内容 发送失败
                    result = 1;                
                    result = QYLED_DLL.SendDateTime_Net(ip, netprotocol, UID, ledcolour, 1, 1, font, size, 0, timeformat, showformat, 0, 0, 0, stoptime);
                    if (result == 0)
                    {
                        MessageBox.Show("下发成功！", "udp发送日期时间");
                    }
                    else
                    {
                        MessageBox.Show("下发失败！", "udp发送日期时间");
                    }
                }
                else if (radioButtonImg.Checked)
                {
                    //udp下发图片组内容
                    result = 1;      
                    result = QYLED_DLL.SendImageGroup_Net(imgpath, ip, netprotocol, width, higth, UID, ledcolour, style, speed, stoptime, 2, save, 1, 1);
                    if (result == 0)
                    {
                        MessageBox.Show("下发成功！", "udp发送图片组");
                    }
                    else
                    {
                        MessageBox.Show("下发失败！", "udp发送图片组");
                    }
                }
            }
            //tcp发送方式
            else if (radioButtonTCP.Checked)
            {
                netprotocol = 2;

                if (radioButtonNB.Checked)
                {
                    //tcp下发内码文字内容
                    result = 1;
                    result = QYLED_DLL.SendInternalText_Net(text, ip, netprotocol, width, higth, UID, ledcolour, style, speed, stoptime, colour, font, size, updatestyle, save);
                    if (result == 0)
                    {
                        MessageBox.Show("下发成功！", "tcp发送内码文字内容");
                    }
                    else
                    {
                        MessageBox.Show("下发失败！", "tcp发送内码文字内容");
                    }
                }
                else if (radioButtonCJ.Checked)
                {
                    //tcp下发实时采集内容
                    result = 1;
                    result = QYLED_DLL.SendCollectionData_Net(text, ip, netprotocol, UID, colour, font, size);
                    if (result == 0)
                    {
                        MessageBox.Show("下发成功！", "tcp发送实时采集内容");
                    }
                    else
                    {
                        MessageBox.Show("下发失败！", "tcp发送实时采集内容");
                    }
                }
                else if (radioButtonPD.Checked)
                {
                    //tcp下发排队叫号内容
                    result = 1;
                    result = QYLED_DLL.SendLineUp_Net(text, ip, netprotocol, stoptime, colour, UID, flash);
                    if (result == 0)
                    {
                        MessageBox.Show("下发成功！", "tcp发送排队叫号内容");
                    }
                    else
                    {
                        MessageBox.Show("下发失败！", "tcp发送排队叫号内容");
                    }
                }
                else if (radioButtonTime.Checked)
                {
                    //tcp下发日期时间内容
                }
                else if (radioButtonImg.Checked)
                {
                    //tcp下发图片组内容
                    result = 1;
                    result = QYLED_DLL.SendImageGroup_Net(imgpath, ip, netprotocol, width, higth, UID, ledcolour, style, speed, stoptime, 1, save, 1, 1);
                    if (result == 0)
                    {
                        MessageBox.Show("下发成功！", "tcp发送图片组");
                    }
                    else
                    {
                        MessageBox.Show("下发失败！", "tcp发送图片组");
                    }
                }
            }
            //串口发送方式
            else if (radioButtonCOM.Checked)
            {
                //
                if (radioButtonNB.Checked)
                {
                    //com下发内码文字内容
                    //QYLED_DLL.mydelegate dele = new QYLED_DLL.mydelegate(comCallBackFun);
                    result = 1;
                    result = QYLED_DLL.SendInternalText_Com(text, rsadr, rstype, comport, baud, width, higth, UID, ledcolour, style, speed, stoptime, colour, font, size, updatestyle, save);
                    if (result == 0)
                    {
                        MessageBox.Show("下发成功！", "下发内码文字内容");
                    }
                    else
                    {
                        MessageBox.Show("下发失败！", "下发内码文字内容");
                    }
                    //QYLED_DLL.SetComCallBack(dele);
                }
                else if (radioButtonCJ.Checked)
                {
                    //com下发送实时采集内容
                    //QYLED_DLL.mydelegate dele = new QYLED_DLL.mydelegate(comCallBackFun);
                    result = 1;
                    result = QYLED_DLL.SendCollectionData_Com(text, rsadr, rstype, comport, baud, UID, colour, font, size);
                    if (result == 0)
                    {
                        MessageBox.Show("下发成功！", "下发实时采集内容");
                    }
                    else
                    {
                        MessageBox.Show("下发失败！", "下发实时采集内容");
                    }
                    //QYLED_DLL.SetComCallBack(dele);
                }
                else if (radioButtonPD.Checked)
                {
                    //com下发排队叫号内容暂无接口
                }
                else if (radioButtonTime.Checked)
                {
                    //无此接口
                }
                else if (radioButtonImg.Checked)
                {
                    //无此接口
                }
            }
            else if (radioButton485.Checked)
            {
                //485
                rstype = 2;
                if (radioButtonNB.Checked)
                {
                    //485下发内码文字内容
                    //QYLED_DLL.mydelegate dele = new QYLED_DLL.mydelegate(comCallBackFun);
                    result = 1;
                    result = QYLED_DLL.SendInternalText_Com(text, rsadr, rstype, comport, baud, width, higth, UID, ledcolour, style, speed, stoptime, colour, font, size, updatestyle, save);
                    if (result == 0)
                    {
                        MessageBox.Show("下发成功！", "下发内码文字内容");
                    }
                    else
                    {
                        MessageBox.Show("下发失败！", "下发内码文字内容");
                    }
                    //QYLED_DLL.SetComCallBack(dele);
                }
                else if (radioButtonCJ.Checked)
                {
                    //485下发送实时采集内容
                    //QYLED_DLL.mydelegate dele = new QYLED_DLL.mydelegate(comCallBackFun);
                    result = 1;
                    result = QYLED_DLL.SendCollectionData_Com(text, rsadr, rstype, comport, baud, UID, colour, font, size);
                    if (result == 0)
                    {
                        MessageBox.Show("下发成功！", "下发实时采集内容");
                    }
                    else
                    {
                        MessageBox.Show("下发失败！", "下发实时采集内容");
                    }
                    //QYLED_DLL.SetComCallBack(dele);
                }
                else if (radioButtonPD.Checked)
                {
                    //485下发排队叫号内容暂无接口
                }
                else if (radioButtonTime.Checked)
                {
                    //无此接口
                }
                else if (radioButtonImg.Checked)
                {
                    //无此接口
                }
            }
        }
        //显示页点播（需建模板）
        private void buttonShowPage_Click_1(object sender, EventArgs e)
        {
            ip = textBoxIP.Text.Trim();                         //ip地址
            ipport = Convert.ToInt32(textBoxIPPort.Text);       //tcp服务端口号
            comport = comboBoxCOMPort.SelectedIndex + 1;        //com端口
            baud = Convert.ToInt32(comboBoxBaudRate.Text);      //波特率
            pagenumber = comboBoxPageNumber.SelectedIndex + 1;  //显示页序号

            if (radioButtonUDP.Checked)
            {
                //udp点播显示页
                netprotocol = 1;
                result = 1;
                result = QYLED_DLL.PlayShowPage_Net(ip, netprotocol, pagenumber);
                if (result == 0)
                {
                    MessageBox.Show("点播成功！", "udp显示页点播");
                }
                else
                {
                    MessageBox.Show("点播失败！", "udp显示页点播");
                }
            }
            else if (radioButtonTCP.Checked)
            {
                //tcp点播显示页
                netprotocol = 2;
                result = 1;
                result = QYLED_DLL.PlayShowPage_Net(ip, netprotocol, pagenumber);
                if (result == 0)
                {
                    MessageBox.Show("点播成功！", "tcp显示页点播");
                }
                else
                {
                    MessageBox.Show("点播失败！", "tcp显示页点播");
                }
            }
            else if (radioButtonCOM.Checked)
            {
                //com点播显示页
                //QYLED_DLL.mydelegate dele = new QYLED_DLL.mydelegate(comCallBackFun);
                result = 1;
                result = QYLED_DLL.PlayShowPage_Com(rsadr, rstype, comport, baud, pagenumber);
                if (result == 0)
                {
                    MessageBox.Show("点播成功！", "显示页点播");
                }
                else
                {
                    MessageBox.Show("点播失败！", "显示页点播");
                }
                //QYLED_DLL.SetComCallBack(dele);
            }
        }
        //设置亮度
        private void buttonSetBright_Click_1(object sender, EventArgs e)
        {
            ip = textBoxIP.Text.Trim();                     //ip地址
            ipport = Convert.ToInt32(textBoxIPPort.Text);   //tcp服务端口
            int priority = 3;                               //亮度优先级固定取值
            brightness = comboBoxBrightness.SelectedIndex;  //亮度等级
            if (radioButtonUDP.Checked)
            {
                //udp设置亮度
                netprotocol = 1;
                result = 1;
                result = QYLED_DLL.SetBright_Net(ip, netprotocol, priority, brightness);
                if (result == 0)
                {
                    MessageBox.Show("设置成功！", "udp置亮度");
                }
                else
                {
                    MessageBox.Show("设置失败！", "udp设置亮度");
                }
            }
            else if (radioButtonTCP.Checked)
            {
                //tcp设置亮度
                netprotocol = 2;
                result = 1;
                result = QYLED_DLL.SetBright_Net(ip, netprotocol, priority, brightness);
                if (result == 0)
                {
                    MessageBox.Show("设置成功！", "tcp设置亮度");
                }
                else
                {
                    MessageBox.Show("设置失败！", "tcp设置亮度");
                }
            }
            else if (radioButtonCOM.Checked)
            {
                //串口调节亮度暂无接口
            }
        }
        //软开屏
        private void buttonStartPlay_Click(object sender, EventArgs e)
        {
            ip = textBoxIP.Text.Trim();                     //ip地址
            if (radioButtonUDP.Checked)
            {
                netprotocol = 1;
                result = 1;
                result = QYLED_DLL.StartPlay_Net(ip, netprotocol);
                if (result == 0)
                {
                    MessageBox.Show("开屏成功！", "UDP开屏");
                }
                else
                {
                    MessageBox.Show("开屏失败！", "UDP开屏");
                }
            }
            else if (radioButtonTCP.Checked)
            {
                //tcp软开屏
                netprotocol = 2;
                result = 1;
                result = QYLED_DLL.StartPlay_Net(ip, netprotocol);
                if (result == 0)
                {
                    MessageBox.Show("开屏成功！", "tcp开屏");
                }
                else
                {
                    MessageBox.Show("开屏失败！", "tcp开屏");
                }
            }
            else if (radioButtonCOM.Checked)
            {
                //串口软开屏暂无接口
            }
        }
        //软关屏
        private void buttonStopPlay_Click(object sender, EventArgs e)
        {
            ip = textBoxIP.Text.Trim();                     //ip地址
            if (radioButtonUDP.Checked)
            {
                netprotocol = 1;
                result = 1;
                result = QYLED_DLL.StopPlay_Net(ip, netprotocol);
                if (result == 0)
                {
                    MessageBox.Show("关屏成功！", "UDP关屏");
                }
                else
                {
                    MessageBox.Show("关屏失败！", "UDP关屏");
                }
            }
            else if (radioButtonTCP.Checked)
            {
                //tcp软开屏
                netprotocol = 2;
                result = 1;
                result = QYLED_DLL.StopPlay_Net(ip, netprotocol);
                if (result == 0)
                {
                    MessageBox.Show("关屏成功！", "tcp关屏");
                }
                else
                {
                    MessageBox.Show("关屏失败！", "tcp关屏");
                }
            }
            else if (radioButtonCOM.Checked)
            {
                //串口软关屏暂无接口
            }
        }
        //校准时间
        private void buttonSendDateTime_Click_1(object sender, EventArgs e)
        {
            ip = textBoxIP.Text.Trim();                     //ip地址
            if (radioButtonUDP.Checked)
            {
                //udp校准时间
                netprotocol = 1;
                result = 1;
                result = QYLED_DLL.TimeCheck_Net(ip, netprotocol);
                if (result == 0)
                {
                    MessageBox.Show("校准成功！", "udp校准时间");
                }
                else
                {
                    MessageBox.Show("校准失败！", "udp校准时间");
                }
            }
            else if (radioButtonTCP.Checked)
            {
                //tcp校准时间
                netprotocol = 2;
                result = 1;
                result = QYLED_DLL.TimeCheck_Net(ip, netprotocol);
                if (result == 0)
                {
                    MessageBox.Show("校准成功！", "tcp校准时间");
                }
                else
                {
                    MessageBox.Show("校准失败！", "tcp校准时间");
                }
            }
            else if (radioButtonCOM.Checked)
            {
                //串口校准时间暂无接口
            }
        }
        //语音播放
        private void buttonPlayVoice_Click(object sender, EventArgs e)
        {
            ip = textBoxIP.Text.Trim();                         //ip地址
            ipport = Convert.ToInt32(textBoxIPPort.Text);       //tcp服务端口号
            comport = comboBoxCOMPort.SelectedIndex + 1;        //com端口
            baud = Convert.ToInt32(comboBoxBaudRate.Text);      //波特率
            voicetext = textBoxVoiceText.Text.Trim();           //语音播放内容
            rsadr = textBoxRSAdr.Text.Trim();                   //485地址

            if (radioButtonUDP.Checked)
            {
                //udp语音播放
                netprotocol = 1;
                result = 1;
                //转发串口1
                if (radioButtonZFCOM1.Checked)
                {
                    result = QYLED_DLL.PlayVoice_Net(voicetext, ip, rsadr, netprotocol, 1);
                }
                //转发串口2
                else if (radioButtonZFCOM2.Checked)
                {
                    result = QYLED_DLL.PlayVoice_Net(voicetext, ip, rsadr, netprotocol, 2);
                }
                //转发485
                else if (radioButtonZF485.Checked)
                {
                    result = QYLED_DLL.PlayVoice_Net(voicetext, ip, rsadr, netprotocol, 3);
                }
                //不转发
                else if (radioButtonBZF.Checked)
                {
                    result = QYLED_DLL.PlayVoice_Net(voicetext, ip, rsadr, netprotocol, 1);
                }

                if (result == 0)
                {
                    MessageBox.Show("播放成功！", "udp语音播放");
                }
                else
                {
                    MessageBox.Show("播放失败！", "udp语音播放");
                }
                
            }
            else if (radioButtonTCP.Checked)
            {
                //tcp语音播放
                netprotocol = 2;
                result = 1;
                //转发串口1
                if (radioButtonZFCOM1.Checked)
                {
                    result = QYLED_DLL.PlayVoice_Net(voicetext, ip, rsadr, netprotocol, 1);
                }
                //转发串口2
                else if (radioButtonZFCOM2.Checked)
                {
                    result = QYLED_DLL.PlayVoice_Net(voicetext, ip, rsadr, netprotocol, 2);
                }
                //转发485
                else if (radioButtonZF485.Checked)
                {
                    result = QYLED_DLL.PlayVoice_Net(voicetext, ip, rsadr, netprotocol, 3);
                }
                //不转发
                else if (radioButtonBZF.Checked)
                {
                    result = QYLED_DLL.PlayVoice_Net(voicetext, ip, rsadr, netprotocol, 1);
                }
                if (result == 0)
                {
                    MessageBox.Show("播放成功！", "tcp语音播放");
                }
                else
                {
                    MessageBox.Show("播放失败！", "tcp语音播放");
                }
            }
            else if (radioButtonCOM.Checked)
            {
                //com语音播放
                rstype = 1;
                //QYLED_DLL.mydelegate dele = new QYLED_DLL.mydelegate(comCallBackFun);
                //串口
                QYLED_DLL.PlayVoice_Com(voicetext, rsadr, rstype, comport, baud);
                //QYLED_DLL.SetComCallBack(dele);
            }
            else if (radioButton485.Checked)
            {
                //485语音播放
                rstype = 3;
                 //QYLED_DLL.mydelegate dele = new QYLED_DLL.mydelegate(comCallBackFun);
                //485
                result = 1;
                result = QYLED_DLL.PlayVoice_Com(voicetext, rsadr, rstype, comport, baud);
                if (result == 0)
                {
                    MessageBox.Show("播放成功！", "语音播放");
                }
                else
                {
                    MessageBox.Show("播放失败！", "语音播放");
                }
                //QYLED_DLL.SetComCallBack(dele);
            }
        }
     
        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

        

        /// <summary>
        /// 实时采集批量下发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMulCollection_Click(object sender, EventArgs e)
        {
            //只支持UDP
            //批量发3条，要分别调用三次：参数分别为：
            //DataIndex=1，DataCount=3；
            //DataIndex=2，DataCount=3；
            //DataIndex=3，DataCount=3；
            //当DataIndex=DataCount等于总条数的话才真正下发数据
            ip = textBoxIP.Text.Trim();
            ledcolour = comboBoxLEDcolour.SelectedIndex;        //led颜色

            if (radioButtonUDP.Checked)
            {
                result = 1;
                result = QYLED_DLL.SendMulCollectionData_Net("一一", ip, 1, 1, ledcolour, 1, 1, 1, 1, 3);
                result = QYLED_DLL.SendMulCollectionData_Net("二二", ip, 1, 2, ledcolour, 1, 1, 1, 2, 3);
                result = QYLED_DLL.SendMulCollectionData_Net("三三", ip, 1, 3, ledcolour, 1, 1, 1, 3, 3);
                if (result == 0)
                {
                    MessageBox.Show("成功！", "udp批量下发实时采集");
                }
                else
                {
                    MessageBox.Show("失败！", "udp批量下发实时采集");
                }
            }
        }

        /// <summary>
        /// 内码文字批量下发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMulText_Click(object sender, EventArgs e)
        {
            //只支持UDP
            //批量发3条，要分别调用三次：参数分别为：
            //DataIndex=1，DataCount=3；
            //DataIndex=2，DataCount=3；
            //DataIndex=3，DataCount=3；
            //当DataIndex=DataCount等于总条数的话才真正下发数据
            ip = textBoxIP.Text.Trim();

            if (radioButtonUDP.Checked)
            {
                result = 1;
                //result = QYLED_DLL.SendMulInternalText_Net("江苏南京 日用电器 10T 10M车 10日", ip, 1, 64, 16, 1, 2, 1, 1, 1, 1, 1, 1, 2, false, 1, 6);
                //result = QYLED_DLL.SendMulInternalText_Net("江苏南通 日用百货 11T 11M车 11日", ip, 1, 64, 16, 2, 2, 1, 1, 1, 1, 1, 1, 2, false, 2, 6);
                //result = QYLED_DLL.SendMulInternalText_Net("江苏无锡 新鲜蔬菜 12T 12M车 12日", ip, 1, 64, 16, 3, 2, 1, 1, 1, 1, 1, 1, 2, false, 3, 6);
                //result = QYLED_DLL.SendMulInternalText_Net("江苏武进 快递业务 13T 13M车 13日", ip, 1, 64, 16, 4, 2, 1, 1, 1, 1, 1, 1, 2, false, 4, 6);
                //result = QYLED_DLL.SendMulInternalText_Net("江苏苏州 散装货物 14T 14M车 14日", ip, 1, 64, 16, 5, 2, 1, 1, 1, 1, 1, 1, 2, false, 5, 6);
                //result = QYLED_DLL.SendMulInternalText_Net("江苏泰州 工厂设备 15T 15M车 15日", ip, 1, 64, 16, 6, 2, 1, 1, 1, 1, 1, 1, 2, false, 6, 6);
                result = QYLED_DLL.SendMulInternalText_Net("测试", ip, 1, 64, 16, 1, 1, 9, 1, 1, 1, 1, 1, 1, false, 1, 2);
                result = QYLED_DLL.SendMulInternalText_Net("欢迎光临", ip, 1, 64, 16, 2, 1, 1, 1, 1, 1, 1, 1, 1, false, 2, 2);
                //result = QYLED_DLL.SendMulInternalText_Net("下发", ip, 1, 64, 16, 3, 0, 1, 1, 1, 1, 1, 1, 2, false, 3, 3);
                if (result == 0)
                {
                    MessageBox.Show("成功！", "udp批量下发内码文字");
                }
                else
                {
                    MessageBox.Show("失败！", "udp批量下发内码文字");
                }
            }
        }

        private void groupBox11_Enter(object sender, EventArgs e)
        {

        }

        private void btnControll_Click(object sender, EventArgs e)
        {
             ip = textBoxIP.Text.Trim();                     //ip地址
            int nCircuitNo = comboBoxCircuitNo.SelectedIndex + 1;
            int nSwitchStatus = comboBoxSwitchStatus.SelectedIndex + 1;

            if (radioButtonUDP.Checked)
            {
                netprotocol = 1;
                result = 1;
                result = QYLED_DLL.RelaySwitch_Net(ip, netprotocol, nCircuitNo, nSwitchStatus);
                if (result == 0)
                {
                    MessageBox.Show("成功！", "udp控制继电器");
                }
                else
                {
                    MessageBox.Show("失败！", "udp控制继电器");
                }

            }
            else if (radioButtonTCP.Checked)
            {
                netprotocol = 2;
                result = 1;
                result = QYLED_DLL.RelaySwitch_Net(ip, netprotocol, nCircuitNo, nSwitchStatus);
                if (result == 0)
                {
                    MessageBox.Show("成功！", "tcp控制继电器");
                }
                else
                {
                    MessageBox.Show("失败！", "tcp控制继电器");
                }
            }
            else if (radioButtonCOM.Checked)
            {
                //串口暂无接口
            }
        }

        private void btnRelay_Click(object sender, EventArgs e)
        {
            ip = textBoxIP.Text.Trim();                     //ip地址
            int nCircuitNo = comboBoxCircuitNo.SelectedIndex + 1;
            int nDelayTime = comboBoxDelayTime.SelectedIndex + 1;

            if (radioButtonUDP.Checked)
            {
                netprotocol = 1;
                result = 1;
                result = QYLED_DLL.RelayDelay_Net(ip, netprotocol, nCircuitNo, nDelayTime);
                if (result == 0)
                {
                    MessageBox.Show("成功！", "继电器延时");
                }
                else
                {
                    MessageBox.Show("失败！", "继电器延时");
                }
            }
            else if (radioButtonTCP.Checked)
            {
                netprotocol = 2;
                result = 1;
                result = QYLED_DLL.RelayDelay_Net(ip, netprotocol, nCircuitNo, nDelayTime);
                if (result == 0)
                {
                    MessageBox.Show("成功！", "继电器延时");
                }
                else
                {
                    MessageBox.Show("失败！", "继电器延时");
                }
            }
            else if (radioButtonCOM.Checked)
            {
                //串口暂无接口
            }
        }
    }
}
